/**
 * Created by apple on 16/2/24.
 */
/*
 |--------------------------------------------------------------------------
 | 模块依赖
 |--------------------------------------------------------------------------
 */

import 'assets/css/libs.min.css';
import 'assets/css/pasp.min.css';
import 'assets/sass/main.scss';

import service from 'app.service';
import interceptor from 'app.interceptor';
import com from 'views/components/index';
import apiService from 'views/api.service';

/*
 |--------------------------------------------------------------------------
 | app 初始化
 |--------------------------------------------------------------------------
 */

let app = angular
    .module('ctg-ae', [ 'ui.bootstrap', 'ui.router', 'pasp.ui', 'ngCookies', com.name ])
    .factory('CommonService', service.factory)
    .factory('systemService', apiService.factory)
    .factory('Interceptor', interceptor.factory)
    .filter('propsFilter', () => {
        return function(items, props) {
            var out = [];
            if (angular.isArray(items)) {
                items.forEach(function(item) {
                    var itemMatches = false;

                    var keys = Object.keys(props);
                    for (var i = 0; i < keys.length; i++) {
                        var prop = keys[i];
                        var text = props[prop].toLowerCase();
                        if (item[prop].toString().toLowerCase().indexOf(text) !== -1) {
                            itemMatches = true;
                            break;
                        }
                    }

                    if (itemMatches) {
                        out.push(item);
                    }
                });
            } else {
                out = items;
            }
            return out;
        }
    })
    .constant('statusMap', { "S0A": "有效", "S0X": "无效"})
    .constant('openMode', { "1000": "框架子页面", "1100": "独立页面"})
    .constant('CodeText', { org: "该组织编码已存在,不能重复添加", staff: "该员工已存在,不能重复添加"})
    .config($httpProvider => $httpProvider.interceptors.push('Interceptor'))
    .config(localStorageServiceProvider => { localStorageServiceProvider.setPrefix('sys') })
    .run(($rootScope, $state, $cookies, localStorageService, uiAlert, $window) => {
        "ngInject"
        let $uiAlert;
        $rootScope.$on('unauthorized', () => {
            if (!$uiAlert) {
                $uiAlert = uiAlert.warning('会话已过期, 请重新登录!', {backdrop: 'static', cancelText: false}).then(() => {
                    $cookies.remove('ctgUser');
                    $cookies.remove('loginType');
                    $rootScope.isLogin = false;
                    $window.location.href = '/ctg-sysmgr-portal-web'
                })
            }
        })
        $rootScope.$on('empty.post', () => {
            if (!$uiAlert) {
                $uiAlert = uiAlert.warning('阁下尚未选择岗位, 即将前往!', {backdrop: 'static', cancelText: false}).then(() => {
                    $state.go('post', {}, { reload: true })
                })
            }
        })
        $rootScope.$on('$stateChangeStart',function(event, toState /*, toParams, fromState, fromParams*/){
            // 如果是进入登录界面则允许
            __DEBUG__ && console.log($rootScope.isLogin)
            let stateName = toState.name;
            if( $.inArray(stateName,['login','reset', 'post']) > -1) {
                $rootScope.isLogin = false;
                return;
            }
            if ($cookies.get('ctgUser')) {
                $rootScope.isLogin = true;
                $rootScope.isMenuLoad = true;
            } else {
                //event.preventDefault();
                $rootScope.isLogin = true;
                $rootScope.isMenuLoad = true;
                //$state.go('login', {}, { reload: true })
            }
        });
    });

/**
 * 页面加载
 */
let pageModules = require.context('./views/', true, /\.index.js$/);
pageModules.keys().forEach(pageModules);

export default app;